;(function($){
    $.fn.retrieve_room_msg = function(opts){
        var el = this;
        var opt = {
            dataBack : function(){},
            closeBack : function(){},
            entityBack : function(){},
        }
        var param = $.extend(opt,opts);
        var app = new App(el,param);
    }

    var App = function(el,param){
        this.el = el;
        this.param = param;
        this.initHtml();
        this.getDs(param.room);
        param.entityBack(this);
    }

    App.prototype = {
        bindEvent : function(){
            var el = this.el;
            var param = this.param;
            var t =  this;

        },
        initHtml : function(){
            var el = this.el;
            var param = this.param;
            var t = this;

            var html =  '<style>'+
                            '.room-msg-form{display:flex;} '+
                            '.room-msg-label{text-align:right;width:20%;margin: 4px 2px;font-size:13px;line-height:22px;} '+
                            '.room-msg-value{width:80%;margin: 4px 2px;font-size:15px;} '+
                            '.room-msg-btn{position: absolute;bottom: 20px;}'+
                            '#room-msg-btn .widget-box{border-top: 1px solid #DCE8F1 !important;}'+
                        '</style>'+
                        '<div class="room-msg-form-container">'+
                            '<div class="room-msg-form">'+
                                
                            '</div>'+
                        '</div>'+
                        '<div class="room-msg-btn">'+
                            '<h5 style="color: #4383B4;padding-left: 3px;">操作</h5>'+
                            '<div id="room-msg-btn"></div>'+
                        '</div>';
            el.html(html);
        },
        initformHtml : function(fds){
            var el = this.el;
            var param = this.param;
            var t = this;
            var labelHtml = '';
            var valueHtml = '';
            var html = "";
            $(fds).each(function(k,v){
                var label = $.parseJSON(v.lable);
                var value = $.parseJSON(v.value);
                if(v.name!="横线"){
                    html +='<div class="room-msg-form">'
                    html += '<div class="room-msg-label">'
                    $(label).each(function(kk,vv){
                        html+= getTextStyle(vv);
                    })
                    html += '</div>'+'<div class="room-msg-value">'
                    $(value).each(function(kk,vv){
                        html+=getTextStyle(vv);
                    })
                    html +='</div>'+'</div>'
                }else{
                    html += '<div style="border-top:1px solid '+(label[0].tcolor||"#e4e6e9")+';margin:0 2px;"></div>'
                }
            });
            el.find(".room-msg-form-container").html(html);
        },
        initButtonHtml : function(bds){
            var el = this.el;
            var param = this.param;
            var t = this;
            var html = '';
            var btypeArr = [];
            el.find("#room-msg-btn").html("");
            $(bds).each(function(k,v){
                var style="min-width:77px;";
                var disable = "";
                
                //判断是否存在分类
                if(btypeArr.indexOf(v.btype)==-1){
                    btypeArr.push(v.btype);
                    var html1 = '<div class="widget-box transparent">'+
                                    '<div class="widget-header hide">'+
                                        '<h5>'+v.btype+'</h5>'+
                                    '</div>'+
                                    '<div class="widget-body">'+
                                        '<div class="widget-main" data-btype="'+v.btype+'" style="padding:5px;"></div>'          
                                    '</div>'+
                                '</div>';
                    el.find("#room-msg-btn").append(html1);
                }
                if(v.bgcolor){
                    style += "background:"+v.bgcolor+" !important;border:2px solid "+v.bgcolor+";"
                }
                if(v.tcolor){
                    style += "color:"+v.tcolor+" !important;"
                }
                if(v.disable==1){
                    disable = "disabled";
                }

                html =  '<div class="btn-group" style="margin:1px;">'+
                            '<button class="btn btn-sm btn-info" data-text="'+v.name+'" style="'+style+'" '+disable+'>'+
                                '<i class="ace-icon '+v.icon+'"></i>'+
                                v.name+
                            '</button>'+
                        '</div>';
                el.find("#room-msg-btn").find(".widget-main[data-btype="+v.btype+"]").append(html);
              
            });
            //测试
            el.find("#room-msg-btn").find("button").unbind("click").bind("click",function(){
                
                // alert($(this).attr("data-text"));
                var text = $(this).data("text");
                var json = $.parseJSON($(".rm-body.active").find(".json").html());
                json.btnName = text;
                json.room = $(".rm-body.active").prev().data("room")
                t.bindOperBtnEvent(json);
            });
            t.setRoomFormH();
        },
        getDs : function(room){
            var el = this.el;
            var param = this.param;
            var t = this;
            this.setSize();
            var arg1 = {"arg":{"room":room}};
            var procedure = "SPAROOMRetreveMsg";
            var arg2 = {};
            $.HSAPI_CALLPROCEDURE2(arg1,procedure,arg2,function(data){
                // console.log(data);
                if(data.RETURN==1){
                    t.initformHtml(data.form);
                    t.initButtonHtml(data.button);
                }
            },function(data){
                console.log(data);
                jf_alert(JSON.stringify(data))
            })
        },
        setSize : function(){
            var el = this.el;
            var param = this.param;
            var t = this;

            var top = el.offset().top;
            // if (self.frameElement && self.frameElement.tagName == "IFRAME") {
                // top = top + 40	
            // }
            var h = $(window).height();
            var hh = h - top;
            el.css({"height":hh});
        },
        setRoomFormH:function(){
            var el = this.el;
            var param = this.param;
            var t = this;
            var tt = el.offset().top;
            var t = el.find(".room-msg-btn").offset().top
            var h = t-tt;
            el.find(".room-msg-form-container").css({"height":h,"overflow-y":"auto"});
        },
        bindOperBtnEvent : function(json){
            let newWindowPrams={'title':json.btnName,param:json,height:810};
            switch (json.btnName) {
                case "开房入住":
                    $.HSAPI_NEWMODAL("/html/window/timesRoom/jquery.window.openRoom.html",newWindowPrams,function(message){
                        // alert(message+'->执行位置为父页面->点击btnFresh');
                        $("#btnFresh").trigger("click");
                    });
                    break;  
                default:
                    $.HSAPI_NEWMODAL("/html/window/example.html",newWindowPrams);
                    break;
            }
        }
    }
})(jQuery);